"""
get a chat connection and extract the chat data.
"""

import chatLogic
from whiff import whiffenv

class getChatData:
    def __init__(self, page):
        self.page = page
    def __call__(self, env, start_response):
        #pr "getChatData called"
        dbprefix = whiffenv.getId(env, "DbPrefix")
        prefix = whiffenv.getName(env)
        #pr "prefix is", (prefix, dbprefix)
        nick = chatLogic.getCookieValue(env)
        startroom = whiffenv.getId(env, "StartRoom", None)
        #pr "getChatData found nick, startroom", (nick, startroom)
        #for pair in sorted(env.items()): #pr pair
        #connection = chatLogic.test_gadfly_connection(dbprefix)
        connection = chatLogic.getConnection(env)
        env = connection.processEnvironment(env, prefix, nick, startroom)
        return self.page(env, start_response)

__middleware__ = getChatData
